﻿Imports System.IO

Public Class Form1
    Public ProjectShortName As String = ""
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim foo As String = ""
        Dim t As Integer = 0
        If OpenFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
            foo = OpenFileDialog1.FileName
            t = Len(OpenFileDialog1.SafeFileName)
            foo = Microsoft.VisualBasic.Left(foo, Len(foo) - t)
            lblJAVAPATH.Text = foo
            SaveSetting("AIToMarket", "Paths", "JAVAPATH", foo)
            If lblKEYSTOREPATH.Text <> "Not Assigned" Then Button11.Enabled = True
        End If

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click

        Dim foo As New Uri("http://code.google.com/p/android-apktool/downloads/list")
        Form2.Show()
        Form2.WebBrowser1.Url = foo
        Form2.WebBrowser1.Navigate(foo)
        MsgBox("You'll need the main APKTools package, and the apk-install-windows package. Extract these to somewhere on your path, like c:\windows")
    End Sub

    Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
        Dim foo As New Uri("http://developer.android.com/sdk/index.html")
        Form2.Show()
        Form2.WebBrowser1.Url = foo
        Form2.WebBrowser1.Navigate(foo)
        MsgBox("Download and unpack the SDk. Make a note of where you put it.")

    End Sub

    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
        Dim foo As New Uri("http://www.oracle.com/technetwork/java/javase/downloads/index.html")
        Form2.Show()
        Form2.WebBrowser1.Url = foo
        Form2.WebBrowser1.Navigate(foo)
        MsgBox("Download and install the JDk (NOT the JRE!!). Make a note of where you put it.")

    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim foo As String = ""
        Dim t As Integer = 0
        If OpenFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
            foo = OpenFileDialog1.FileName
            t = Len(OpenFileDialog1.SafeFileName)
            foo = Microsoft.VisualBasic.Left(foo, Len(foo) - t)
            lblANDROIDSDKTOOLSPATH.Text = foo
            SaveSetting("AIToMarket", "Paths", "ANDROIDSDKTOOLSPATH", foo)
        End If

    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Dim foo As String = ""
        Dim t As Integer = 0
        If OpenFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
            foo = OpenFileDialog1.FileName
            t = Len(OpenFileDialog1.SafeFileName)
            foo = Microsoft.VisualBasic.Left(foo, Len(foo) - t)
            lblAPKTOOLSPATH.Text = foo
            SaveSetting("AIToMarket", "Paths", "APKTOOLSPATH", foo)
        End If

    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        Dim foo As String = ""
        Dim t As Integer = 0
        If OpenFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
            foo = OpenFileDialog1.FileName
            t = Len(OpenFileDialog1.SafeFileName)
            foo = Microsoft.VisualBasic.Left(foo, Len(foo) - t)
            lblPROJECTPATH.Text = foo
            SaveSetting("AIToMarket", "Paths", "PROJECTPATH", foo)
        End If

    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        lblANDROIDSDKTOOLSPATH.Text = GetSetting("AIToMarket", "Paths", "ANDROIDSDKTOOLSPATH", "Not Assigned")
        lblAPKTOOLSPATH.Text = GetSetting("AIToMarket", "Paths", "APKTOOLSPATH", "Not Assigned")
        lblJAVAPATH.Text = GetSetting("AIToMarket", "Paths", "JAVAPATH", "Not Assigned")
        lblPROJECTPATH.Text = GetSetting("AIToMarket", "Paths", "PROJECTPATH", "Not Assigned")
        lblKEYSTOREPATH.Text = GetSetting("AIToMarket", "Paths", "KEYSTOREPATH", "Not Assigned")
        If lblJAVAPATH.Text <> "Not Assigned" And lblKEYSTOREPATH.Text <> "Not Assigned" Then Button11.Enabled = True
        ValidatePaths()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        ValidatePaths()
    End Sub

    Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12.Click
        Dim foo As String = ""
        Dim t As Integer = 0
        If OpenFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
            foo = OpenFileDialog1.FileName
            t = Len(OpenFileDialog1.SafeFileName)
            foo = Microsoft.VisualBasic.Left(foo, Len(foo) - t)
            lblKEYSTOREPATH.Text = foo
            SaveSetting("AIToMarket", "Paths", "KEYSTOREPATH", foo)
            If lblJAVAPATH.Text <> "Not Assigned" Then Button11.Enabled = True
        End If
    End Sub

    Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
        Dim foo As String = ""
        foo = lblJAVAPATH.Text + "\keytool -genkey -alias "
        Dim q As String = InputBox("Provide a name for this key (no spaces or punctuation):", "Key Name", "MyKey")
        foo = foo + q + ".keystore -keyalg RSA -validity 20000 -keystore " + lblKEYSTOREPATH.Text + "\" + q + ".keystore"
        Shell(foo, AppWinStyle.NormalFocus, True)

        'keytool -genkey -alias yourname.keystore -keyalg RSA -validity 20000 -keystore keys/yourname.keystore
    End Sub

    Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
        
        Dim foo As String
        lblStepMessage.Text = "Select your AppInventor APK file."
        OpenFileDialog1.Filter = "*APK files|*.apk"
        OpenFileDialog1.FilterIndex = 2
        OpenFileDialog1.FileName = "*.apk"
        OpenFileDialog1.InitialDirectory = lblPROJECTPATH.Text
        If OpenFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
            foo = OpenFileDialog1.FileName
            lblWorkingAPK.Text = foo
            Button13.Enabled = True
        End If

    End Sub

    Private Sub Button13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button13.Click
        '%APKTOOLSPATH%\apktool.bat d -s %apkname%.apk
        lblStepMessage.Text = "Unpacking APK file..."
        Microsoft.VisualBasic.ChDir(lblPROJECTPATH.Text)
        Dim foo As String
        foo = lblAPKTOOLSPATH.Text + "apktool.bat d -s " + lblWorkingAPK.Text
        Shell(foo, AppWinStyle.NormalFocus, True)
        Button14.Enabled = True
        Kill(lblWorkingAPK.Text)
    End Sub

    Private Sub Button14_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button14.Click
        'Edit manifest
        Dim foo As String
        foo = Microsoft.VisualBasic.Left(lblWorkingAPK.Text, Len(lblWorkingAPK.Text) - 4)
        Microsoft.VisualBasic.ChDir(foo)
        Shell("notepad AndroidManifest.xml", AppWinStyle.NormalFocus, False)
        Form3.Show()

    End Sub

    Private Sub Button15_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button15.Click
        Dim foo As String
        Dim foo2 As String
        lblStepMessage.Text = "Repacking APK..."
        foo = Microsoft.VisualBasic.Left(lblWorkingAPK.Text, Microsoft.VisualBasic.Len(lblWorkingAPK.Text) - 4)
        Dim t As Integer = 0
        Do
            t = Microsoft.VisualBasic.InStr(foo, "\")
            If t = 0 Then
                Exit Do
            Else
                foo = Microsoft.VisualBasic.Right(foo, Len(foo) - t)
            End If
        Loop
        ProjectShortName = foo
        Microsoft.VisualBasic.ChDir(lblPROJECTPATH.Text)
        foo2 = lblAPKTOOLSPATH.Text + "apktool.bat b " + foo
        Shell(foo2, AppWinStyle.NormalFocus, True)
        '%APKTOOLSPATH%\apktool b .\%apkname%
        Button16.Enabled = True

    End Sub

    Private Sub Button16_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button16.Click
        '%JAVABINPATH%\jarsigner -verbose -keystore %KEYSTOREPATH%\%keystorename% -signedjar .\_signed\%apkname%.apk .\%apkname%\dist\%apkname%.apk %keystorename%
        Dim foo As String
        foo = Microsoft.VisualBasic.Left(lblWorkingAPK.Text, Len(lblWorkingAPK.Text) - 4)
        Dim SafeKeyName As String = ""
        Dim Keyfile As String = ""
        Microsoft.VisualBasic.ChDir(foo + "\dist")
        lblStepMessage.Text = "Signing APK file....."
        OpenFileDialog1.Filter = "Keystore files|*.keystore"
        OpenFileDialog1.FilterIndex = 2
        OpenFileDialog1.FileName = "*.keystore"
        OpenFileDialog1.InitialDirectory = lblKEYSTOREPATH.Text
        If OpenFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
            Keyfile = OpenFileDialog1.FileName
            SafeKeyName = OpenFileDialog1.SafeFileName
        End If
        Dim ExecString As String
        ExecString = lblJAVAPATH.Text + "jarsigner.exe -verbose -keystore " + Keyfile + " -signedjar ..\" + ProjectShortName + ".apk " + foo + "\dist\" + ProjectShortName + ".apk " + SafeKeyName
        Shell(ExecString, AppWinStyle.NormalFocus, True)
        Button17.Enabled = True

    End Sub

    Private Sub Button17_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button17.Click
        '%SDKTOOLSPATH%\zipalign -v 4 .\_signed\%apkname%.apk .\_finished\%apkname%.apk
        Dim foo As String
        foo = Microsoft.VisualBasic.Left(lblWorkingAPK.Text, Len(lblWorkingAPK.Text) - 4)
        Microsoft.VisualBasic.ChDir(foo)
        Dim ExecString As String = ""
        ExecString = lblANDROIDSDKTOOLSPATH.Text + "zipalign.exe -v 4 " + ProjectShortName + ".apk ..\" + ProjectShortName + ".apk"
        Shell(ExecString, AppWinStyle.NormalFocus, True)
        Button18.Enabled = True

    End Sub

    Private Sub Button18_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button18.Click
        Microsoft.VisualBasic.ChDir(lblPROJECTPATH.Text)
        If File.Exists(lblPROJECTPATH.Text + ProjectShortName + ".apk") Then
            MsgBox("Market-ready APK successfully processed! Click ok to clean up mess and open file location.")
            Shell("Explorer " + lblPROJECTPATH.Text, AppWinStyle.NormalFocus, False)

        Else
            MsgBox("Something has gone terribly wrong! Click ok to clean up the mess and start over. You'll need to put a fresh copy of your AppInventor app in the project folder.")
        End If
        Directory.Delete(lblPROJECTPATH.Text + ProjectShortName, True)
    End Sub
End Class
